#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

#define KEY 6
char *keyword[KEY] = { "for", "if", "then", "else", "while", "do" };
char id[1024];
int num;
int token;
int idl=0; 

void lexical(FILE *fp) {
	
	char x = getc(fp);
	
	while (isspace(x) != 0&&x!=EOF)
		x = getc(fp);

	if(x==EOF||x=='#'){
		token=0;
		strcpy(id,"#");
		return;
	} 

	idl=0;
	strcpy(id, "\0");
	if ((x>='0' && x<='9')||(x>='A' && x<='Z')||(x>='a' && x<='z')) {
		while (x!=EOF) {
			if ((x=='#')||(!((x>='0' && x<='9')||(x>='A' && x<='Z')||(x>='a' && x<='z')))) {
				ungetc(x, fp);
				break;
			}
			id[idl++]=x;
			x=getc(fp);
		}
		id[idl]='\0';
		//printf("__________%s_________\n",id);
		if (strcmp(id, "for") == 0) {
			token = 1;
		}
		else if (strcmp(id, "if") == 0) {
			token = 2;
		}
		else if (strcmp(id, "then") == 0) {
			token = 3;
		}
		else if (strcmp(id, "else") == 0) {
			token = 4;
		}
		else if (strcmp(id, "while") == 0) {
			token = 5;
		}
		else if (strcmp(id, "do") == 0) {
			token = 6;
		}

		else {
			if ((num = atoi(id)) != 0) {
				token = 11;
			}
			else {
				if (strcmp(id, "0") == 0) {
					token = 11;
					num = 0;
				}
				else {
					if (!((id[0] >= 'A'&&id[0]<='Z') || (id[0]>='a'&&id[0] <= 'z'))) {
						//printf("ERROR__________%s_________\n",id);
						token = -1;
					}
					else {
						token = 10;
					}
				}
			}
		}
	}

	else if (!((x>='0' && x<='9')||(x>='A' && x<='Z')||(x>='a' && x<='z'))) {
		while (x!=EOF) {
			if ((x>='0' && x<='9')||(x>='A' && x<='Z')||(x>='a' && x<='z')||(isspace(x)!=0)||(x=='#')) {
				ungetc(x, fp);
				break;
			}
			id[idl++]=x;
			x=getc(fp);
		}
		id[idl]='\0';
		if (strcmp(id, "+") == 0) {
			token = 13;
		}

		else if (strcmp(id, "-") == 0) {
			token = 14;
		}
		else if (strcmp(id, "*") == 0) {
			token = 15;
		}
		else if (strcmp(id, "/") == 0) {
			token = 16;
		}
		else if (strcmp(id, ":") == 0) {
			token = 17;
		}
		else if (strcmp(id, ":=") == 0) {
			token = 18;
		}
		else if (strcmp(id, "<") == 0) {
			token = 20;
		}
		else if (strcmp(id, "<>") == 0) {
			token = 21;
		}
		else if (strcmp(id, "<=") == 0) {
			token = 22;
		}
		else if (strcmp(id, ">") == 0) {
			token = 23;
		}
		else if (strcmp(id, ">=") == 0) {
			token = 24;
		}
		else if (strcmp(id, "=") == 0) {
			token = 25;
		}
		else if (strcmp(id, ";") == 0) {
			token = 26;
		}
		else if (strcmp(id, "(") == 0) {
			token = 27;
		}
		else if (strcmp(id, ")") == 0) {
			token = 28;
		}
		else if(strcmp(id, "#") == 0){
			token=0;
		}
		else {
			//printf("ERROR_____*****_____%s_________\n",id);
			token = -1;
		}
	}
	else;
	//num = 0;
	

	//process x
	//process each regex here
	//store the result in global variables id, num, or token
	//in case of backtrack, use
	//ungetc(x, fp);
	
	//idl=0;
	//id[1023]='\0';
}

int main(int argc, const char *argv[])
{
	if (argc < 2) {
		printf("insufficient args\n");
		return 1;
	}
	FILE *fp = fopen(argv[1], "r");
	do {
		lexical(fp);
		switch (token) {
		case 10: printf("(id, %s)\n", id); break;
		case 11: printf("(num, %d)\n", num); break;
		case -1: printf("error!\n"); break;
		default: printf("(%d, %s)\n", token, id);
		}
	} while (token != 0);


	fclose(fp);
	return 0;
}



//while (isspace(x) != 1) {
//		strcat(id, x);
//		x = getc(fp);
//	}

	
